'''
归并排序
1.分解:将列表越分越小,直至分成一个元素
2.终止条件:一个元素是有序的
3.合并:将两个有序列表合并,列表越来越大
'''

# 时间复杂度O(nlogn)
# 空间复杂度O(n)
def merge(li, low, mid, high):
    i = low
    j = mid + 1
    ltmp = []
    while i <= mid and j <= high:  # 只要两边都有数
        if li[i] < li[j]:
            ltmp.append(li[i])
            i += 1
        else:
            ltmp.append(li[j])
            j += 1
    # while 执行完,有一部分没数了
    while i <= mid:
        ltmp.append(li[i])
        i += 1
    while j <= high:
        ltmp.append(li[j])
        j += 1
    # 把值写会去
    li[low:high + 1] = ltmp
    return ltmp


def merge_sort(li, low, high):
    if low < high:  # 至少有两个元素
        mid = (low + high) // 2
        merge_sort(li, low, mid)
        merge_sort(li, mid + 1, high)
        merge(li, low, mid, high)


import random

li = list(range(100))
random.shuffle((li))
print(li)
merge_sort(li, 0, len(li) - 1)
print(li)
